package com.example.springboot.controller.user;

import com.example.springboot.entity.Res;
import com.example.springboot.entity.UserWatchRemember;
import com.example.springboot.entity.vo.PostVo;
import com.example.springboot.service.UserWatchRememberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/userWatchRemember")
public class UserWatchRememberController {

    @Autowired
    private UserWatchRememberService userWatchRememberService;

    @PostMapping("/add")
    public Res<String> addUserWatchRemember(@RequestBody UserWatchRemember userWatchRemember) {
        try {
            int result = userWatchRememberService.addUserWatchRemember(userWatchRemember);
            if (result > 0) {
                return new Res<String>().code(200).msg("Add success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Add failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Res<String> deleteUserWatchRemember(@PathVariable int id) {
        try {
            int result = userWatchRememberService.deleteUserWatchRemember(id);
            if (result > 0) {
                return new Res<String>().code(200).msg("Delete success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Delete failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @PutMapping("/update")
    public Res<String> updateUserWatchRemember(@RequestBody UserWatchRemember userWatchRemember) {
        try {
            int result = userWatchRememberService.updateUserWatchRemember(userWatchRemember);
            if (result > 0) {
                return new Res<String>().code(200).msg("Update success").data("Success");
            } else {
                return new Res<String>().code(500).msg("Update failed").data("Failure");
            }
        } catch (Exception e) {
            return new Res<String>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @GetMapping("/get/{id}")
    public Res<UserWatchRemember> getUserWatchRememberById(@PathVariable int id) {
        try {
            UserWatchRemember result = userWatchRememberService.getUserWatchRememberById(id);
            if (result != null) {
                return new Res<UserWatchRemember>().code(200).msg("Get success").data(result);
            } else {
                return new Res<UserWatchRemember>().code(404).msg("Not found").data(null);
            }
        } catch (Exception e) {
            return new Res<UserWatchRemember>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }

    @GetMapping("/getByUserId/{userId}")
    public Res<List<PostVo>> getUserWatchRememberByUserId(@PathVariable int userId,
                                                          @RequestParam(defaultValue = "1") Integer pageNum,
                                                          @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            List<PostVo> result = userWatchRememberService.getUserWatchRememberByUserId(userId, pageNum, pageSize);
            if (result != null && !result.isEmpty()) {
                return new Res<List<PostVo>>().code(200).msg("Get success").data(result);
            } else {
                return new Res<List<PostVo>>().code(404).msg("No records found").data(result);
            }
        } catch (Exception e) {
            return new Res<List<PostVo>>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }
    @GetMapping("/getTotalNumByUserId/{userId}")
    public Res<Integer> getUserWatchRememberNumByUserId(@PathVariable int userId) {
        try {
            Integer result = userWatchRememberService.getUserWatchRememberNumByUserId(userId);
            if (result != null) {
                return new Res<Integer>().code(200).msg("Get success").data(result);
            } else {
                return new Res<Integer>().code(404).msg("No records found").data(result);
            }
        } catch (Exception e) {
            return new Res<Integer>().code(500).msg("Error occurred: " + e.getMessage()).data(null);
        }
    }
}